package com.xuecheng.learning.mq;

import com.alibaba.fastjson.JSON;
import com.xuecheng.framework.domain.task.XcTask;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.learning.config.RabbitMQconfig;
import com.xuecheng.learning.service.XcLearningService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: gongfy
 * @create: 2020/9/25 16:31
 * @Description:
 */
@Component
public class ChooseCourseTask {
    @Autowired
    private XcLearningService xcLearningService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = RabbitMQconfig.XC_LEARNING_ADDCHOOSECOURSE)
    public void receiveChoosecourseTask(XcTask xcTask) {
        //1.解析xcTask中的requestBody
        String requestBody = xcTask.getRequestBody();
        Map msgMap = JSON.parseObject(requestBody, Map.class);
        String userId = String.valueOf(msgMap.get("userId"));
        String courseId = String.valueOf(msgMap.get("courseId"));
        //2.添加选课   learning_course、task_his表添加数据
        ResponseResult addcourse = xcLearningService.addCourse(userId, courseId, null, null, xcTask);
        //3.添加成功则向订单服务发送消息
        if (addcourse.isSuccess()) {
            rabbitTemplate.convertAndSend(RabbitMQconfig.EX_LEARNING_ADDCHOOSECOURSE, RabbitMQconfig.XC_LEARNING_FINISHADDCHOOSECOURSE_KEY, xcTask);
        }
    }

}
